package edu.ubb.bibliospring.backend.service.bean;

import edu.ubb.bibliospring.backend.exception.BiblioSpringRepositoryException;
import edu.ubb.bibliospring.backend.exception.BiblioSpringServiceException;
import edu.ubb.bibliospring.backend.model.Author;
import edu.ubb.bibliospring.backend.repository.AuthorRepository;
import edu.ubb.bibliospring.backend.service.AuthorService;
import javax.ejb.DependsOn;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import java.util.List;
import javax.annotation.security.DeclareRoles;
import javax.annotation.security.RolesAllowed;

@Stateless(name = "AuthorService", mappedName = "ejb/AuthorService")
@DependsOn({"AuthorRepository", "Author"})
//@DeclareRoles({"super","users"})
public class AuthorServiceBean implements AuthorService {

    @EJB
    private AuthorRepository authorRepository;

//    @RolesAllowed("super")
    @Override
    public List<Author> getAll() throws BiblioSpringServiceException {
        try {
            return authorRepository.getAll();
        } catch (final BiblioSpringRepositoryException bbre) {
            throw new BiblioSpringServiceException("Get all author: ", bbre);
        }
    }

    @Override
    public Author getById(final Long id) throws BiblioSpringServiceException {
        try {
            return authorRepository.getById(id);
        } catch (final BiblioSpringRepositoryException e) {
            throw new BiblioSpringServiceException("Get author by id: ", e);
        }
    }

    @Override
    public Author insertAuthor(final Author author) throws BiblioSpringServiceException {
        try {
            return authorRepository.save(author);
        } catch (final BiblioSpringRepositoryException e) {
            throw new BiblioSpringServiceException("Insert a new author: ", e);
        }
    }

    @Override
    public void deleteAuthor(Author author) throws BiblioSpringServiceException {
        try {
            authorRepository.delete(author);
        } catch (final BiblioSpringRepositoryException e) {
            throw new BiblioSpringServiceException("Delete author: ", e);
        }
    }
}
